package com.bw.a1908.uitls;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.ScaleAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ScreenUtils;
import com.bw.a1908.R;

/**
 * 点赞
 * 1.视频点赞
 * 2.用户点赞
 * <p>
 * 1.主心 放大 透明度
 */
public class GoodView extends FrameLayout {


    public GoodView(@NonNull Context context) {
        super(context);
        init();
    }

    public GoodView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            View view = createHart(new Point((int) event.getX(), (int) event.getY()));
            addView(view);
            runAnimHart(view);
        }
        return super.onTouchEvent(event);
    }

    /**
     * 创建心形View
     * Point 创建位置
     */
    private ImageView createHart(Point point) {
        ImageView hartIv = new ImageView(getContext());
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(300, 300);
        params.leftMargin = point.x - 150;
        params.topMargin = point.y - 150;
        hartIv.setLayoutParams(params);
        hartIv.setImageResource(R.mipmap.cab);
        return hartIv;
    }

    /**
     * 执行组合动画
     */
    private void runAnimHart(View view) {
        ObjectAnimator animatorX = ObjectAnimator.ofFloat(view, "ScaleX", 1, 2);
        ObjectAnimator animatorY = ObjectAnimator.ofFloat(view, "ScaleY", 1, 2);
        ObjectAnimator animatorA = ObjectAnimator.ofFloat(view, "Alpha", 1, 0);

        AnimatorSet set = new AnimatorSet();
        set.setDuration(2000);
        set.play(animatorX).with(animatorY).with(animatorA);
        set.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                destroyHart(view);
            }
        });
        set.start();
    }

    /**
     * 销毁
     */
    private void destroyHart(View view) {
        //视图销毁
        removeView(view);
    }
}
